﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Windows.Input;
using System.Windows;

using Resxus.Model;
using Resxus.DataAccess;

namespace Resxus.ViewModel
{
    public class MenuBarVM : ViewModelBase
    {

    #region Fields

        IEdit _control;
        ICollection<ResourceFile> _repository;

    #endregion //Fields

    #region Constructor

        public MenuBarVM(IEdit resources)
        {
            _control = resources;
            _repository = resources.Files;
        }

    #endregion //Constructor

    #region Commands

        /// <summary>
        /// Open Command 
        /// </summary>
        ICommand _OpenCommand = null;
        public ICommand OpenCommand
        {
            get
            {
                if (_OpenCommand == null)
                {
                    _OpenCommand = new RelayCommand(param => Open());
                }
                return _OpenCommand;
            }
        }

        /// <summary>
        /// Add Command
        /// </summary>
        ICommand _AddCommand = null;
        public ICommand AddCommand
        {
            get
            {
                if (_AddCommand  == null)
                {
                    _AddCommand  = new RelayCommand(pararm => Add());
                }
                return _AddCommand;
            }
        }       

        /// <summary>
        /// Save Command 
        /// </summary>
        ICommand _SaveCommand = null;
        public ICommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new RelayCommand(param => Save());
                }
                return _SaveCommand;
            }
        }

        /// <summary>
        /// Save All Command
        /// </summary>
        ICommand _SaveAllCommand = null;
        public ICommand SaveAllCommand
        {
            get
            {
                if (_SaveAllCommand  == null)
                {
                    _SaveAllCommand = new RelayCommand(pararm => SaveAll());
                }
                return _SaveAllCommand;
            }
        }
  
        /// <summary>
        /// Exit Command
        /// </summary>
        ICommand _ExitCommand = null;
        public ICommand ExitCommand
        {
            get
            {
                if (_ExitCommand == null)
                {
                    _ExitCommand = new RelayCommand(param => Exit());
                }
                return _ExitCommand;
            }
        }


        ICommand _FindCommand = null;
        public ICommand FindCommand
        {
            get
            {
                if (_FindCommand  == null)
                {
                    _FindCommand  = new RelayCommand(pararm => Find());
                }
                return _FindCommand;
            }
        }

        ICommand _FilterCommand = null;
        public ICommand FilterCommand
        {
            get
            {
                if (_FilterCommand  == null)
                {
                    _FilterCommand  = new RelayCommand(pararm => Filter());
                }
                return _FilterCommand;
            }
        }
                
    #endregion //Commands

    #region Binding Properties
    #endregion //Binding Properties

    #region Private Helpers


    #endregion //Private Helpers

    #region Public Methods
        
        /// <summary>
        /// Open new file 
        /// </summary>
        public void Open()
        {
            this._control.Load(_repository, "Open Resource Files", (file) => 
            {
                _repository.Add(file);
                _control.Open(file);
            });           
        }

        /// <summary>
        /// Add file
        /// </summary>
        void Add()
        {
            this._control.Load(_repository, "Add Resource Files", (file) =>
            {
                _repository.Add(file);                
            });           
        }

        /// <summary>
        /// Save current selected file
        /// </summary>
        public void Save()
        {
            ResourceEditVM selected = _control.Current as ResourceEditVM;
            if (selected != null)
            {
                ResourceFile file = selected.File;
                _control.Save(file);
            }            
        }

        /// <summary>
        /// Save all opened files 
        /// </summary>
        void SaveAll()
        {
            foreach (var item in _control.Files)
            {
                _control.Save(item);
            } 
        }

        
        /// <summary>
        /// Close program
        /// </summary>
        public void Exit()
        {
            System.Windows.Application.Current.Shutdown();
        }

        /// <summary>
        /// Find Record
        /// </summary>
        void Find()
        {
            Resxus.View.FindAndReplaceWindowView findView = new Resxus.View.FindAndReplaceWindowView();            
            findView.DataContext = new FindAndReplaceWindowVM();
            findView.ShowDialog();
        }

        /// <summary>
        /// Filter on Current Records 
        /// </summary>
        void Filter()
        {

        }

    #endregion //Public Methods

    }
}
